<?php

namespace addons\cms\controller\api;


use addons\cms\model\Archives as ArchivesModel;
use addons\cms\model\ArchivesInteraction;
use addons\cms\model\ScoreLog;
use addons\cms\model\SocietiesSign;
use addons\third\model\Third;
use app\admin\model\cms\ActivitySign;
use think\Db;
use think\Validate;

/**
 * 文档
 */
class Societies extends Base
{
    protected $noNeedLogin = ['index', 'detail', 'get_channel', 'lists', 'comment_list'];


    public function index()
    {
        $societies = \addons\cms\model\Societies::field('id,title,title_zh_cn,title_en,image,logo_image,views,content,content_zh_cn,content_en')
            ->where('id', 1)
            ->whereNull('deletetime')
            ->find();
        $lang_default = getDefaultLang();
        if ($lang_default !=getHeaderLang()){
            $lang_key = getHeaderLang() == 'zh-cn' ? 'zh_cn' : getHeaderLang();
            $societies->title = $societies['title_'.$lang_key];
            $societies->content = $societies['content_'.$lang_key];
        }
        $societies->setInc('views');
        $societies['image_text'] = cdnurl($societies->image, true);
        $societies['logo_image_text'] = cdnurl($societies->logo_image, true);
        $societies->archives_count = \addons\cms\model\Archives::where('status', 'normal')
            ->where('societies_id', $societies->id)
            ->whereNull('deletetime')
            ->count() ?? 0;
        $societies->activity_count = \addons\cms\model\Activity::where('status', 'normal')
            ->where('societies_id', $societies->id)
            ->whereNull('deletetime')
            ->count() ?? 0;
        $this->success('', $societies->toArray());
    }


    public function sign()
    {

        $row = $this->request->post('', '', 'trim,xss_clean');

        $rule = [
            'name|姓名' => 'require',
            'mobile|手机号' => 'require',
            'mobile_code|验证码' => 'require',
            'gender|性别' => 'require',
            'user_address|地址' => 'require',
            'email|邮箱' => 'require',
        ];

        $msg = [
            'name.require' => '请填写姓名',
            'mobile.require' => '请填写手机号',
            'mobile_code.require' => '请填写验证码',
            'gender.require' => '性别不能为空',
            'user_address.require' => '请填写地址',
            'email.require' => '请填写邮箱',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($row);
        if (!$result) {
            $this->error($validate->getError());
        }

        $info = \addons\cms\model\SocietiesSign::where('user_id', $this->auth->id)
            ->find();
        if ($info) {
            $this->error('你已经入会');
        }

        Db::startTrans();
        try {
            $data = [
                'name' => $row['name'],
                'mobile' => $row['mobile'],
                'user_address' => $row['user_address'],
                'email' => $row['email'],
                'user_id' => $this->auth->id,
                'createtime' => time(),
                'gender' => $row['gender'],
                'societies_id' => 1
            ];
            SocietiesSign::create($data);

            $societies = new \addons\cms\model\Societies();

            $societies_no = $societies->memberCode(11);

            \addons\cms\model\User::where('id', $this->auth->id)->update([
                'is_societies' => 1,
                'societies_no' => $societies_no
            ]);
            //增加积分
            $score = ScoreLog::saveLog('societies', $this->auth->id, 1);
            Db::commit();
            $this->success('',
                ['societies_info' => $data, 'score' => $score]);
        } catch (Exception $e) {
            Db::rollback();
            $this->error('报名失败');
        }

    }

    public function comment_list()
    {
        $id = $this->request->param('id');
        $info = \addons\cms\model\Activity::where('status', 'normal')
            ->where('id', $id)
            ->field('updatetime,deletetime', true)
            ->find();
        if (!$info) {
            $this->error('The activity does not exist');
        }
        $list = \addons\cms\model\Comment::with('user')->where('status', 'normal')
            ->where('aid', $id)
            ->field('id,user_id,content,ip_address,createtime')
            ->where('type', 'activity')
            ->order('createtime desc')
            ->paginate(10);

        $this->success('Get ahead', $list->toArray());
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 我的活动
     */
    public function my_sign()
    {
        $pageList = \addons\cms\model\Activity::where('status', 'normal')
            ->field('id,title,image,description,start_time,end_time,address,attend_num')
            ->whereNull('deletetime')
            ->order('weigh DESC,createtime DESC')
            ->select();

        if ($pageList) {
            foreach ($pageList as $key => $item) {
                if ($this->auth->id) {
                    $sign = ActivitySign::where(['user_id' => $this->auth->id, 'activity_id' => $item->id])->find();
                    if ($sign) {
                        unset($pageList[$key]);
                    }
                }
                $item->sign_num = ActivitySign::where('activity_id', $item->id)->count();
                $item->sign_remaining = $item->attend_num - $item->sign_num;
            }
        }

        $my_sign = ActivitySign::where('user_id', $this->auth->id)->order('createtime desc')->select();
        $this->success('', ['my_sign' => $my_sign->toArray(), 'sign_list' => $pageList->toArray()]);

    }

}
